function doDelete(ajax_url){
	var list = $(".delCheckbox:checked");
	if(list.length == 0){
		showChoseItemDialog();
		return false;
	}
	
	var del_ids = "";
	for(var i=0; i<list.length; i++){
		if(del_ids != "")
			del_ids += ",";
		del_ids += $(list[i]).val();
	}
	
	//add table data
	var datas = "del_ids=" + del_ids;	
	//show confirm
	$( "#dialog:ui-dialog" ).dialog( "destroy" );
	$( "#dialog-confirm" ).dialog({
		resizable: false,
		height:140,
		modal: true,
		buttons: {
			"Delete": function() {
				$("#dialog-confirm").dialog( "close" );
				$.ajax({
					  url: ajax_url,
					  type: "POST",
					  data: datas,
					  success: function(data) {
						  showDeleteSuccessfulDialog();
						  return false;
					  },
					  complete: function(){	
						  
					  },
					  error: function() {
						  alert("Have error when loading data from server");
					  }
				});
			},
			Cancel: function() {
				$( this ).dialog( "close" );
			}
		}
	});
	
	return false;
}

function showDeleteSuccessfulDialog(){
	$( "#dialog:ui-dialog" ).dialog( "destroy" );
	$( "#dialog-delete-successful" ).dialog({
		resizable: false,
		height:140,
		modal: true,
		buttons: {
			OK: function() {
				$( this ).dialog( "close" );
				window.location.reload();
			}
		}
	});
}

function showChoseItemDialog(){
	$( "#dialog:ui-dialog" ).dialog( "destroy" );
	$( "#dialog-please-chose-item" ).dialog({
		resizable: false,
		height:140,
		modal: true,
		buttons: {
			OK: function() {
				$( this ).dialog( "close" );
			}
		}
	});
}

$(document).ready(function(){
	$("#line_per_page").change(function(){
		refreshTable();
	});
});

function doChangePage(page){
	$("#page").val(page);
	refreshTable();
}

function doChangeValue(name){
	var wrapper_id = "#" + name + "_value";
	var hidden_field = "#" + name + "_change";
	
	//mark as changed
	$(hidden_field).val(1);
	
	//hide preview & show edit
	$(wrapper_id + " .preview").css('display', 'none');
	$(wrapper_id + " .editValue").css('display', '');
	$(wrapper_id + " #change_button").css('display', 'none');
	$(wrapper_id + " #cancel_button").css('display', '');;
}

function doCancelValue(name){
	var wrapper_id = "#" + name + "_value";
	var hidden_field = "#" + name + "_change";
	
	//mark as changed
	$(hidden_field).val(0);
	
	//hide preview & show edit
	$(wrapper_id + " .preview").css('display', '');
	$(wrapper_id + " .editValue").css('display', 'none');
	$(wrapper_id + " #change_button").css('display', '');
	$(wrapper_id + " #cancel_button").css('display', 'none');;
}

function doRedirect(url){
	window.location = url;
}

function changeDeleteControl(){
	if($('#del_ctrl').is(':checked')){
		$('.delCheckbox').attr('checked', true);
	}else{
		$('.delCheckbox').attr('checked', false);		
	}
}

function doAdd(add_url){
	var add_params = $("#add_params").val();
	var add_param_arr = add_params.split(",");

	for(var i=0; i < add_param_arr.length; i++){
		var param = add_param_arr[i]; 
		if($("#" + param).val()){
			add_url = add_url + "/" + param + "/" + $("#" + param).val(); 
		}
	}
	
	window.location = add_url;
}